package com.zhuxiang.client;

import com.zhuxiang.model.Question;
import com.zhuxiang.server.ServerDao;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;

public class ClientQueDao {
    private List<Question> queList;
    public ClientQueDao(List<Question> queList){
        this.queList = queList;
    }
    /**
     * 判断考题id是否已存在
     * @param id
     * @return
     */
    private int isExists(int id) throws IOException, ClassNotFoundException {
        queList = new ServerDao(queList).readExam();
        for (int i = 0; i < queList.size(); i++) {
            Question q = queList.get(i);
            if (id == q.getId()){
                return i;
            }
        }
        return -1;
    }

    /**
     * 查询考题
     */
    public void queryQuestion() throws IOException, ClassNotFoundException {
        queList = new ServerDao(queList).readExam();
        if (queList.size() == 0){
            System.out.println("尚无信息");
        }else {
            for (Question q: queList) {
                System.out.println(q);
            }
        }
    }

    /**
     * 更新考题
     */
    public void updateQuestion() throws IOException, ClassNotFoundException {
        System.out.println("请输入考题id");
        int id = ClientScanner.getSc().nextInt();
        int index = isExists(id);
        if (-1 != index){
            System.out.println("请输入考题");
            String program = ClientScanner.getSc().next();
            System.out.println("请输入答案");
            String answer = ClientScanner.getSc().next();
            Question q = new Question(id,program,answer);
            queList.set(index,q);
            new ServerDao(queList).writeExam();
            System.out.println("信息修改成功");
        }else {
            System.out.println("输入的id可能有误");
        }
    }


    /**
     * 删除考题
     */
    public void deleteQuestion() throws IOException, ClassNotFoundException {
        System.out.println("请输入删除考题的id");
        int id = ClientScanner.getSc().nextInt();
        int index = isExists(id);
        if (-1 != index){
            queList.remove(index);
            new ServerDao(queList).writeExam();
            System.out.println("删除成功");
        }else {
            System.out.println("输入id可能有误");
        }
    }

    /**
     * 导入考题
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void importQuestion() throws IOException, ClassNotFoundException {
        queList = new ServerDao(queList).readExam();
        File file = new File("import.txt");
        if (!file.exists()){
            System.out.println("文件不存在");
        }
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        List<Question> qlist1 = (List<Question>) ois.readObject();
        for (int i = 0; i < qlist1.size(); i++) {
            if (!queList.contains(qlist1.get(i))){
                queList.add(qlist1.get(i));
            }
        }
        new ServerDao(queList).writeExam();
        ois.close();
        System.out.println("导入试题成功");
    }

    /**
     * 新增考题
     */
    public void addQuestion() throws IOException, ClassNotFoundException {
        int id = 0;
        while (true){
            System.out.println("请输入考题id");
            id = ClientScanner.getSc().nextInt();
            //先判断该id是否已存在
            int index = isExists(id);
            if (-1 != index){
                System.out.println("该id已存在");
            }else {
                break;
            }
        }
        System.out.println("请输入考题");
        String program = ClientScanner.getSc().next();
        System.out.println("请输入答案");
        String answer = ClientScanner.getSc().next();
        Question q = new Question(id,program,answer);
        queList.add(q);
        new ServerDao(queList).writeExam();
        System.out.println("信息添加成功");
    }
}
